import VueRouter from "vue-router"
import store from "@/store"

const router = new VueRouter({
    routes: [
        {
            path: "/",
            redirect: "/Home"
        },
        {
            path: '/Home',                                          //首页
            name: "Home",
            component: () => import("@/views/Home/index.vue"),
            meta: { show: true }        //底部组件是否在使用
        },
        {
            path: "/Login",                                         //登录
            component: () => import("@/views/Login/index.vue"),
            meta: { show: false },

        },
        {

            path: "/Register",                                      //注册
            component: () => import("@/views/Register/index.vue"),
            meta: { show: false },

        },
        {
            path: "/Search/:keyword",
            name: "Search",                                         //搜索
            component: () => import("@/views/Search/index.vue"),
            meta: { show: true },
        },
        {
            path: "/Detaile/:skuid",                                 //详情
            name: "Detaile",
            component: () => import("@/views/Detail/index.vue"),
            meta: { show: true }
        },
        {
            path: "/AddCartSuccess",                                //加入购物车模组
            name: 'AddCartSuccess',
            component: () => import('@/views/AddCartSuccess/index.vue'),
            meta: { show: true }

        },
        {
            path: "/ShopCart",                                      //购物车组件
            name: "ShopCart",
            component: () => import("@/views/ShopCart/index.vue"),
            meta: { show: true }
        },
        {
            path: '/Trade',                                         //订单详情
            name: 'Trade',
            component: () => import("@/views/Trade/index.vue"),
            meta: { show: true },
            beforeEnter: (to, form, next) => {
                if (form.path == "/ShopCart") {
                    next();
                } else {
                    next(false);
                }
            }


        },
        {
            path: '/Pay',                                           //订单支付页
            name: 'Pay',
            component: () => import("@/views/Pay/index.vue"),
            meta: { show: true },
            beforeEnter: (to, form, next) => {
                if (form.path == "/Trade") {
                    next();
                } else {
                    next(false);
                }
            }
        },
        {
            path: '/PaySuccess',                                        //订单支付成功的组件
            name: "PaySuccess",
            component: () => import("@/views/PaySuccess/index.vue"),
            meta: { show: true },
            beforeEnter: (to, form, next) => {
                if (form.path == "/Pay") {
                    next();
                } else {
                    next(false);
                }
            }
        },
        {
            path: '/Center',                                            //我的订单
            name: 'Center',
            component: () => import("@/views/Center/index.vue"),
            meta: { show: true },
            children: [                 //二级路由
                {
                    path: 'MyOrder',
                    component: () => import("@/views/Center/MyOrder/index.vue"),
                },
                {
                    path: 'groupOrder',
                    component: () => import("@/views/Center/groupOrder/index.vue")
                },
                {
                    path: '/Center',
                    redirect: "/Center/MyOrder"
                }
            ]

        }
    ],
    scrollBehavior(to, from, savedPosition) {           //滚动行为操作，路由跳转之后
        return { y: 0 }
    }
})


// 导航守卫  限制
router.beforeEach(async (to, from, next) => {
    let token = store.state.LoginandRegistration.token;
    let UseInfoname = store.state.LoginandRegistration.UseInfodata.name;
    var topath = ["/ShopCart", "/AddCartSuccess", "/Center", "/Center/MyOrder", "/Center/groupOrder",
        "/PaySuccess", "/Trade"];
    if (token) {                                                //登录后
        if (to.path == "/Login" || to.path == "/Register") {
            next("/Home");
        } else {
            if (UseInfoname) {
                next();
            } else {
                try {
                    await store.dispatch("UseInfo");              //在路由守卫中派发网络请求
                    next();
                } catch (err) {
                    await localStorage.removeItem("token");
                    state.dispatch("logout");
                    next("/Login");

                }

            }
        }
    }
    else {                              //未登录的操作
        // decodeURIComponent("%2FCenter%2FMyOrder")        读取url中带的参数
        let topa = to.path;
        console.log(topa);
        topath.forEach(toitem => {
            if (to.path == toitem) {
                next("/Login?With=" + topa);
            }
            else {
                next()
            }
        })

    }

})
export default router;